package com.heluo.eduservice.controller;


import com.heluo.commonutils.R;
import com.heluo.eduservice.entity.EduCourse;
import com.heluo.eduservice.entity.vo.CourseInfoVo;
import com.heluo.eduservice.entity.vo.CoursePublicVo;
import com.heluo.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-10
 */
@Api(description="课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;


    @GetMapping
    public R GetCourseList(){

        List<EduCourse> list=courseService.list(null);

        return R.ok().data("list",list);
    }

    //分页查询
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable long current,@PathVariable long limit,
                        @RequestBody(required = false) EduCourse eduCourse){
        Page<EduCourse> page=new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();

        String title=eduCourse.getTitle();
        String status=eduCourse.getStatus();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status","1".equals(status)?"Normal":"Draft");
        }

        courseService.page(page,wrapper);

        long total=page.getTotal();
        List<EduCourse> records=page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @DeleteMapping("{id}")
    public R deleteCourese(@PathVariable String id){
        courseService.removeCourse(id);
        return R.ok();
    }
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id=courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublicVo coursePublicVo=courseService.PublishCourseInfo(id);
            return R.ok().data("publishCourse",coursePublicVo);
        }
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

}

